// pages/index/index.js
import {
  getCanteenList,
  getBannerList,
  getFoodClass,
  getFoodList,
  addSiteIntofoodList,
  findCanteenIdByName,
  findCanteenNameById,
  timeNowToTimeNumber
} from "../../apis/apis"
var app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    pickerIndex: 0,
    array: ['菜名', '评分', '价格'],
    classArr: [],
    bannerArr: [],
    canteenArr: [],
    foodArr: [],
    currentCanteenID: 0,
    currentCanteenName: "",
    inputValue: "",
    inputRemind: "请输入菜名...",
    activeKey: 0,
    pageNo: 1,
    showFoodArr: [],
    showRemind: "",
    timeArr: ["早餐", "午餐", "晚餐", "宵夜"]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.showBannerPic();
    this.showCanteenList();
    this.timeToRemind();

  },

  //搜索的分类切换更新
  pickerChange: function (e) {
    var tempText
    if (e.detail.value == 0) tempText = "请输入菜名..."
    else if (e.detail.value == 1) tempText = "评分不低于..."
    else if (e.detail.value == 2) tempText = "价格不高于..."
    this.setData({
      pickerIndex: e.detail.value,
      inputRemind: tempText
    })
  },
  //获取轮播图数据
  async showBannerPic() {
    this.setData({
      bannerArr: await getBannerList()
    })
  },
  //初次获取全部饭堂+侧边栏+菜单的清单
  async showCanteenList() {
    //获取饭堂列表
    await getCanteenList().then(res => {
      app.globalData.canteenList = res
      this.setData({
        canteenArr: app.globalData.canteenList,
        currentCanteenID: app.globalData.canteenList[0].id,
        currentCanteenName: app.globalData.canteenList[0].canteenName,
      })
    }).catch()

    //获取饭堂列表中第一个饭堂的所有菜品分类
    await getFoodClass(app.globalData.canteenList[0].id).then(res => {
      app.globalData.foodClassList = res
      this.setData({
        classArr: res.reverse()
      })
      console.log(this.data.classArr);
    })
    //获取饭堂列表中第一个饭堂的第一个菜品分类下的所有菜品
    await getFoodList(1, 10, app.globalData.canteenList[0].id, this.data.classArr[0].id).then(res => {
      console.log(res);
      let showArr = []
      for (let index = 0; index < res.rows.length; index++) {
        let foodTime = res.rows[index].time
        if (foodTime.indexOf(timeNowToTimeNumber()) !== -1) {
          showArr.push(res.rows[index])
        }
      }
      addSiteIntofoodList(this.data.currentCanteenName, showArr)
      this.setData({
        foodArr: res.rows,
        showFoodArr: showArr
      })
      console.log(res.total);

    })

  },
  //饭堂Tab栏切换
  async tabChange(e) {
    this.setData({
      pageNo: 1,
      activeKey:0
    })
    var newCanteenId = findCanteenIdByName(e.detail.title, app.globalData.canteenList)
    //对侧边栏做一个清空以及刷新当前canteenID和canteenName
    this.setData({
      currentCanteenID: newCanteenId,
      currentCanteenName: e.detail.title,
      classArr: []
    })
    //获取菜品分类列表
    await getFoodClass(newCanteenId).then(res => {
      this.setData({
        classArr: res.reverse(),
      })
    })
    //获取第一个分类的全部菜品
    await getFoodList(1, 6, newCanteenId, this.data.classArr[0].id).then(res => {
      let showArr = []
      for (let index = 0; index < res.rows.length; index++) {
        let foodTime = res.rows[index].time
        if (foodTime.indexOf(timeNowToTimeNumber()) !== -1) {
          showArr.push(res.rows[index])
        }
      }
      addSiteIntofoodList(this.data.currentCanteenName, showArr)
      this.setData({
        foodArr: res.rows,
        showFoodArr: showArr
      })
      if(this.data.showFoodArr.length < 5 && this.data.foodArr.length < res.total)
      {
        this.continueLoad();
      }
    })
  },

  //侧边栏（菜品分类）切换
  async sidebarChange(e) {
    this.setData({
      pageNo:1,
      activeKey:e.detail
    })
    await getFoodList(this.data.pageNo, 6, this.data.currentCanteenID, this.data.classArr[e.detail].id).then(res => {
      let showArr = []
      for (let index = 0; index < res.rows.length; index++) {
        let foodTime = res.rows[index].time
        if (foodTime.indexOf(timeNowToTimeNumber()) !== -1) {
          showArr.push(res.rows[index])
        }
      }
      addSiteIntofoodList(this.data.currentCanteenName, showArr)
      console.log(res);
      console.log("展示菜品", showArr);
      this.setData({
        foodArr: res.rows,
        showFoodArr: showArr
      })
      if(this.data.showFoodArr.length < 5 && this.data.foodArr.length < res.total)
      {
        this.continueLoad();
      }

    })
  },

  //跳转搜索页面
  goSearch() {
    if(this.data.inputValue!=""){

    
    console.log(parseInt(this.data.pickerIndex) + 1);
    console.log("inputVal" + this.data.inputValue);
    wx.navigateTo({
      url: '/pages/search/search?category=' + (parseInt(this.data.pickerIndex) + 1) + '&data=' + this.data.inputValue,
    })
  }
  else{
    wx.showToast({
      title: '输入内容为空',
      icon:"error"
    })
  }
},
  //输入框内容双向绑定
  inputChange: function (e) {
    // console.log(e.detail.value);
    this.setData({
      inputValue: e.detail.value
    })
  },
  continueLoad() {
    this.setData({
      pageNo: this.data.pageNo + 1
    })
    getFoodList(this.data.pageNo, 6, this.data.currentCanteenID, this.data.classArr[this.data.activeKey].id).then(res => {
      console.log(res);
      let showArr = []
      addSiteIntofoodList(this.data.currentCanteenName, res.rows)

      for (let index = 0; index < res.rows.length; index++) {
        let foodTime = res.rows[index].time
        if (foodTime.indexOf(timeNowToTimeNumber()) !== -1) {
          showArr.push(res.rows[index])
        }
      }
      let newShowArr = this.data.showFoodArr.concat(showArr)
      let oldFoodArr = this.data.foodArr
      let newFoodArr = oldFoodArr.concat(res.rows)
      console.log(newFoodArr);
      this.setData({
        foodArr: newFoodArr,
        showFoodArr: newShowArr
      })
      if(this.data.showFoodArr.length < 5 && this.data.foodArr.length < res.total)
      {
        this.continueLoad();
      }
    })
  },
  timeToRemind() {
    const now = new Date()
    const hour = now.getHours()
    const min = now.getMinutes()
    let showRemindOne = ""
    if (parseInt(timeNowToTimeNumber()) == 1) {
      if ((hour == 6 && min >= 45) || hour == 7 || hour == 8) {
        showRemindOne = "现在是早餐时间"
      } else {
        showRemindOne = '现在饭堂处于未营业状态，展示列表为早餐'
      }
    } else if (parseInt(timeNowToTimeNumber()) == 2) {
      if ((hour == 10 && min < 30) || hour==9) {
        showRemindOne = '现在饭堂处于未营业状态，展示列表为午餐'
      } else {
        showRemindOne = "现在是午餐时间"
      }
    } else if (parseInt(timeNowToTimeNumber()) == 3) {
      if ((hour == 16 && min < 30) || (hour==12 && min >=45) || (hour>=13 && hour<=15) ) {
        showRemindOne = '现在饭堂处于未营业状态，展示列表为晚餐'
      } else {
        showRemindOne = "现在是晚餐时间"
      }
    } else if (parseInt(timeNowToTimeNumber()) == 4) {
      showRemindOne = "现在是宵夜时间"
    } else {
      this.setData({
        showRemind: '现在饭堂处于未营业状态'
      })
    }
    this.setData({
      showRemind: showRemindOne
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {


  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    this.continueLoad();
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },

  //点击菜单项
  tapFoodItem() {
    this.setData({
      "animations.foodItem": tapAnimation()
    })
  }
})